package image.test.karcio;

import java.awt.Graphics;
import java.awt.Image;
import java.io.File;

import javax.imageio.ImageIO;
import javax.swing.JComponent;

public class ImageComponent extends JComponent {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2660347261567375161L;
	private Image image;

	public ImageComponent() {

		try {
			image = ImageIO.read(new File(
					"/home/karcio/git/java-apps-karcio/SwingCourse/images/blue-ball.gif"));
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public void paintComponent(Graphics g) {

		if (image == null)
			return;

		int imageWidth = image.getWidth(this);
		int imageHeight = image.getHeight(this);

		g.drawImage(image, 0, 0, null);

		for (int i = 0; i * imageWidth <= getWidth(); i++)
			for (int j = 0; j * imageHeight <= getHeight(); j++)
				if (i + j > 0)
					g.copyArea(0, 0, imageWidth, imageHeight, i * imageWidth, j
							* imageHeight);

	}
}
